package main

// 参考 https://www.infoq.cn/article/golang-standard-library-part02

import (
	"fmt"
	"net/http"
	"net/http/cgi"
	"strings"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		handler := new(cgi.Handler)
		handler.Path = "/usr/local/go/bin/go" //可执行的二进制文件或脚本
		fmt.Println(handler.Path)

		//cgi 只能处理go文件 因为最终处理方式为 go run /path/to/xxx.go
		if strings.Contains(r.URL.Path, ".go") == false{
			return
		}

		basePath := "/data/golang/src/go-play/standardLib/cgi/exec"
		script := basePath + r.URL.Path //被执行的脚本或文件
		handler.Dir = basePath //cgi程序的工作路径
		args := []string{"run", script}
		handler.Args = append(handler.Args, args...) // 最终变为 go run /path/to/client/main.go
		fmt.Println(handler.Args)

		handler.ServeHTTP(w, r)
	})

	http.ListenAndServe(":8989", nil)
	select {}
}
